<template>
  <el-select
    :filterable="filterable"
    style="width: 100%"
    :clearable = "clearable"
    @input="change($event)"
    v-model="value">
    <el-option
      v-for="item in list"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
</template>
<script>
  export default {
    name: "CySelect",
    props:{
      // 默认数据
      list: {
        type: Array,
        default: () => []
      },
      // 是否增加全部选项
      allOption:{
        type:Boolean,
        default:false
      },
      // 是否可删除
      clearable:{
        type:Boolean,
        default:false
      },
      // 绑定值
      value: {
        type: [String, Object, Number]
      },
      // 请求地址
      url:{
        type:String
      },
      filterable:{
        type:Boolean,
        default:false
      },
      // 请求参数
      requestBody:{
        type: Object,
        default: () => {}
      },
      // 请求方式
      method:{
        type:String,
        default:'GET'
      }
    },
    data () {
      return {
      }
    },
    methods: {
      change(val) {
        this.$emit('input', val)
      }
    },
    mounted() {
      if(this.url){
        let method = this.method.toUpperCase();
        this.utils.request.liveRequest({
          url: this.url,
          method: method,
          params: this.requestBody,
        }).then(result => {
          let data = result.data;
          if (data.code === '0') {
            let dataList = data.data;
            if(this.allOption){
              this.list=[]
              this.list.push({
                label: '全部'
              })
              dataList.forEach(item=>{
                this.list.push(item)
              })
            } else {
              this.list = dataList
            }
          }
        })
      }
    }
  }
</script>
